package cn.kailangtu.myapplication.data;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import cn.kailangtu.myapplication.enums.ActionCode;

public class CanTAG {
    /**
     * 用于特殊值，比如换档杆的5：R,10:D,255故障等
     * 标识V,
     * 例V5,V0_1,V10
     */
    private List<Integer> 对比值;

    /**
     * 用于标识使用什么动作
     * 决定了使用哪个资源
     * 标识：A
     * 举例:A2
     */
    private Integer 动作;
    /**
     * 用于标识使用资源的第几个
     * 标识:S
     */
    private Integer 资源索引;
    /**
     * 使用第几个单位：
     * 标识:U
     */
    private String 单位;
    /**
     * 0,是循环式刷新；1是值变化式刷新
     * 标识:R
     *
     */
    private int 刷新模式 = 0;
    private ActionCode actionCode= null;


    private List<String> ls_文字集合 = null;
    private List<Integer> ls_图片集合 = null;
    private List<Integer> ls_颜色集合 = null;
    private List<Integer> ls_显示 = null;
    private List<CustomItem> ls_自定义 = null;
    private List<MatchItem> ls_值匹配 = null;
    private List<SoundItem> ls_声音 = null;


    public static CanTAG build(String source)
    {
        CanTAG result = new CanTAG();

        result.set动作(getInnerIntValue(source,"A"));
        result.set资源索引(getInnerIntValue(source,"S"));
        result.set单位("");

        Integer unit = getInnerIntValue(source,"U");
        List<Integer> value = getInnerIntArray(source,"V");
        Integer refreshMode = getInnerIntValue(source,"R");
        if(refreshMode != null)
        {
            result.set刷新模式(refreshMode.intValue());
        }

        if(unit != null)
        {
            result.set单位(CanDataItemSet.getInstance().get单位(unit));
        }
        if(value != null)
        {
            Log.d("SHOOTLEE_CANTAG","对比值："+value);
            result.set对比值(value);
        }


        if (result.get动作() != null && result.get动作() > 0) {
            result.setActionCode(ActionCode.fromValue(result.get动作()));
        }

        if (result.get资源索引() != null && result.get资源索引() > -1) {
            if (source.startsWith("A1")) {
                result.setLs_文字集合(CanDataItemSet.getInstance().get文字集合(result.get资源索引()));
            } else if (source.startsWith("A2") || source.startsWith("A3")) {
                //A2,A3是图片
                result.setLs_图片集合(CanDataItemSet.getInstance().get图片集合(result.get资源索引()));
            } else if (source.startsWith("A4") || source.startsWith("A5")) {
                //A4,A5是颜色
                result.setLs_颜色集合(CanDataItemSet.getInstance().get颜色集合(result.get资源索引()));
            } else if (source.startsWith("A6")) {
                //A6是显示
                result.setLs_显示(CanDataItemSet.getInstance().get显示(result.get资源索引()));
            } else if(source.startsWith("A7"))
            {
                //A7值匹配
                result.setLs_值匹配(CanDataItemSet.getInstance().ls_值匹配.get(result.get资源索引()));
            } else if(source.startsWith("A8")){
                //A8自定义
                result.setLs_自定义(CanDataItemSet.getInstance().ls_自定义.get(result.get资源索引()));
            }else if(source.startsWith("A9"))
            {
                result.setLs_声音(CanDataItemSet.getInstance().ls_声音.get(result.get资源索引()));
            }

        }

        return result;
    }

    public Integer get动作() {
        return 动作;
    }

    public void set动作(Integer 动作) {
        this.动作 = 动作;
    }

    public Integer get资源索引() {
        return 资源索引;
    }

    public void set资源索引(Integer 资源索引) {
        this.资源索引 = 资源索引;
    }

    public String get单位() {
        return 单位;
    }

    public void set单位(String 单位) {
        this.单位 = 单位;
    }

    static String[] element = new String[]{"A","S","U","R","V"};

    private static String getInnerString(String source,String startChar)
    {
        String result = "";

        if(source != null && source.indexOf(startChar)>-1)
        {
            source = source.substring(source.indexOf(startChar)+1);
            for(String c : element){
                if(source.indexOf(c)> -1)
                {
                    source = source.substring(0,source.indexOf(c));
                }
            }
            result = source;
        }

        return result;
    }

    private static List<Integer> getInnerIntArray(String source,String startChar)
    {
        Log.d("GETALLCHILD", "SOURCE:" + source+" ||| STARTCHAR:"+startChar);
        List<Integer> result = new ArrayList<>();
        String strResult = getInnerString(source,startChar);
        if(strResult.length()>0)
        {
            if(strResult.indexOf("_")> -1)
            {
                String[] arrayValue = strResult.split("_");
                for(String valueItem : arrayValue)
                {
                    result.add(Integer.valueOf(valueItem));
                }
            }
            else{
                result.add(Integer.valueOf(strResult));
            }
        }
        if(result.size()>0)
        {
            return result;
        }
        return null;
    }


    private static Integer getInnerIntValue(String source,String startChar)
    {
        Log.d("GETALLCHILD", "SOURCE:" + source+" ||| STARTCHAR:"+startChar);
        Integer result = null;
        String strResult = getInnerString(source,startChar);
        if(strResult.length()>0)
        {
            if(strResult.startsWith("_"))
            {
                strResult = strResult.substring(1);
                int intResult = Integer.valueOf(strResult);
                result = 0-intResult;
            }
            else{
                result = Integer.valueOf(strResult);;
            }
        }
        return result;
    }


    public List<String> getLs_文字集合() {
        return ls_文字集合;
    }

    public void setLs_文字集合(List<String> ls_文字集合) {
        this.ls_文字集合 = ls_文字集合;
    }

    public List<Integer> getLs_图片集合() {
        return ls_图片集合;
    }

    public void setLs_图片集合(List<Integer> ls_图片集合) {
        this.ls_图片集合 = ls_图片集合;
    }

    public List<Integer> getLs_颜色集合() {
        return ls_颜色集合;
    }

    public void setLs_颜色集合(List<Integer> ls_颜色集合) {
        this.ls_颜色集合 = ls_颜色集合;
    }

    public List<Integer> getLs_显示() {
        return ls_显示;
    }

    public void setLs_显示(List<Integer> ls_显示) {
        this.ls_显示 = ls_显示;
    }

    public ActionCode getActionCode() {
        return actionCode;
    }

    public void setActionCode(ActionCode actionCode) {
        this.actionCode = actionCode;
    }

    public int get刷新模式() {
        return 刷新模式;
    }

    public void set刷新模式(int 刷新模式) {
        this.刷新模式 = 刷新模式;
    }

    public List<Integer> get对比值() {
        return 对比值;
    }

    public void set对比值(List<Integer> 对比值) {
        this.对比值 = 对比值;
    }

    public List<CustomItem> getLs_自定义() {
        return ls_自定义;
    }

    public void setLs_自定义(List<CustomItem> ls_自定义) {
        this.ls_自定义 = ls_自定义;
    }

    public List<MatchItem> getLs_值匹配() {
        return ls_值匹配;
    }

    public void setLs_值匹配(List<MatchItem> ls_值匹配) {
        this.ls_值匹配 = ls_值匹配;
    }

    public List<SoundItem> getLs_声音() {
        return ls_声音;
    }

    public void setLs_声音(List<SoundItem> ls_声音) {
        this.ls_声音 = ls_声音;
    }
}
